package bdz.projects.cbt.core
{
	import flash.events.EventDispatcher;

	public class CBTCash extends EventDispatcher
	{
		private var _amount:Number = 0;
		
		public function CBTCash(){
			super();
		}
		private function numFormatted(num:Number,decimalPlace:Number=2,currency:String="$"):String{
            //assigns true boolean value to neg in number less than 0
            var neg:Boolean = (num < 0);
            
            //make the number positive for easy conversion
            num = Math.abs(num)

            var roundedAmount:String = String(num.toFixed(decimalPlace));
            
            //split string into array for dollars and cents
            var amountArray:Array = roundedAmount.split(".");
            var dollars:String = amountArray[0]
            var cents:String = amountArray[1]
            
            //create dollar amount
            var dollarFinal:String = ""
            var i:int = 0
            for (i; i < dollars.length; i++){
                if (i > 0 && (i % 3 == 0 )){
                    dollarFinal = "," + dollarFinal;
                }
                
                dollarFinal = dollars.substr( -i -1, 1) + dollarFinal;
            }   
            
            //create Cents amount and zeros if necessary
            var centsFinal:String = String(cents);
            
            var missingZeros:int = decimalPlace - centsFinal.length;
        
            if (centsFinal.length < decimalPlace){
                for (var j:int = 0; j < missingZeros; j++){
                    centsFinal += "0";
                }
            }
            
            var finalString:String = ""

            if (neg){
                finalString = "-"+currency + dollarFinal
            }else{
                finalString = currency + dollarFinal
            }

            if(decimalPlace > 0){
                finalString += "." + centsFinal;
            } 
            
            return finalString;
        }
		public function removeAmount(n:Number):Number{
			_amount -= n;
			return _amount;
		}
		public function addAmount(n:Number):Number{
			_amount += n;
			return _amount;
		}
		public function get value():Number{
			return _amount;
		}
		public function set value(n:Number):void{
			_amount = n;
		}
	
		public function get formattedValue():String{
			return numFormatted(_amount, 0);
		}
	}
}